/*
 * AttributeParser.java
 *
 * Created on November 4, 2005, 3:11 PM
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.interaction3d.assembly.sax;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.LocatorImpl;

/**
 * @author Michael Nischt
 * @version 0.1
 */
class AttributeParser
{    
    // <editor-fold defaultstate="collapsed" desc=" Variables ">
    
    private final Locator locator;
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" Constructors ">
    
    /** Creates a new instance of AttributeParser */
    public AttributeParser(Locator locator) {
        if(locator == null) {
            this.locator = new LocatorImpl();
        }
        else
            this.locator = locator;
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" parseFloat ">
    
    public float parseFloat(Attributes attributes, int index) throws SAXParseException {
        try {
            return Float.valueOf(attributes.getValue(index));
        } catch(NumberFormatException e) {
            throw new SAXParseException(null, this.locator, e);
        } catch(NullPointerException e) {
            throw new SAXParseException(null, this.locator, e);
        }
    }
    
    public float parseFloat(Attributes attributes, int index, float value) throws SAXParseException {
        final String s = attributes.getValue(index);
        if(s == null)
            return value;        
        try {
            return Float.valueOf(s);
        } catch(NumberFormatException e) { throw new SAXParseException(null, this.locator, e); }
    }      
    
    public float parseFloat(Attributes attributes, String name) throws SAXParseException {
        try {
            return Float.valueOf(attributes.getValue(name));
        } catch(NumberFormatException e) {
            throw new SAXParseException(null, this.locator, e);
        } catch(NullPointerException e) {
            throw new SAXParseException(null, this.locator, e);
        }
    }
    
    public float parseFloat(Attributes attributes, String name, float value) throws SAXParseException {
        final String s = attributes.getValue(name);
        if(s == null)
            return value;        
        try {
            return Float.valueOf(s);
        } catch(NumberFormatException e) { throw new SAXParseException(null, this.locator, e); }
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" parseInt ">
    
    public int parseInt(Attributes attributes, int index) throws SAXParseException {
        try {
            return Integer.valueOf(attributes.getValue(index));
        } catch(NumberFormatException e) {
            throw new SAXParseException(null, this.locator, e);
        } catch(NullPointerException e) {
            throw new SAXParseException(null, this.locator, e);
        }
    }
    
    public int parseInt(Attributes attributes, int index, int value) throws SAXParseException {
        final String s = attributes.getValue(index);
        if(s == null)
            return value;
        try {
            return Integer.valueOf(s);
        } catch(NumberFormatException e) { throw new SAXParseException(null, this.locator, e); }
    }     
    
    public int parseInt(Attributes attributes, String name) throws SAXParseException {
        try {
            return Integer.valueOf(attributes.getValue(name));
        } catch(NumberFormatException e) {
            throw new SAXParseException(null, this.locator, e);
        } catch(NullPointerException e) {
            throw new SAXParseException(null, this.locator, e);
        }
    }
    
    public int parseInt(Attributes attributes, String name, int value) throws SAXParseException {
        final String s = attributes.getValue(name);
        if(s == null)
            return value;
        try {
            return Integer.valueOf(s);
        } catch(NumberFormatException e) { throw new SAXParseException(null, this.locator, e); }
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" parseString ">    
    
    public String parseString(Attributes attributes, String name, String value) throws SAXParseException {
        final String val = attributes.getValue(name);
        if(val != null)
            return val;
        else
            return value;        
    }
    
    public String parseString(Attributes attributes, String name) throws SAXParseException {
        final String val = attributes.getValue(name);
        if(val != null)
            return val;
        else
            throw new SAXParseException(null, this.locator, new NullPointerException());
    }
    
    // </editor-fold>
}
